package com.sw.paisong.controller;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sw.paisong.entity.StaffEntity;
import com.sw.paisong.service.StaffService;
import com.sw.paisong.util.FormUtils;
import com.sw.paisong.util.ServletUtils;
import com.sw.paisong.util.SessionUtil;
import com.sw.paisong.vo.BootstrapTableParamsVO;
import com.sw.paisong.vo.JsonVO;
import com.sw.paisong.vo.StaffFormVO;

@Controller
@RequestMapping(value = "/staff")
public class StaffController {

	private static final Logger logger = LoggerFactory.getLogger(StaffController.class);

	@Autowired
	StaffService staffService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getStaffList(HttpServletResponse response) {
		return "staff_list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public void getStaffListData(BootstrapTableParamsVO bootstrapTableParams, HttpServletResponse response) {
		logger.debug("post-/staff/list");
		boolean isSuccess = true;
		String message = "";
		Page<StaffEntity> staffs = null;
		try {
			PageRequest pageRequest = new PageRequest(bootstrapTableParams.getOffset()
					/ bootstrapTableParams.getLimit(), bootstrapTableParams.getLimit(), new Sort(Sort.Direction.DESC,
					"createAt"));
			staffs = staffService.getStaffPage(pageRequest);
			for (StaffEntity staff : staffs) {
				logger.info(staff.getVPhone());
			}
		} catch (Exception ex) {
			isSuccess = false;
			message = ex.toString();
			logger.debug(ex.toString());
		}
		JsonVO<Page<StaffEntity>> resJson = new JsonVO<Page<StaffEntity>>();
		resJson.setSuccess(isSuccess);
		resJson.setData(staffs);
		resJson.setMessage(message);
		ServletUtils.responseJson(response, resJson);
	}

	/**
	 * 新增一个员工
	 * 
	 * @param response
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void saveStaff(StaffFormVO staffFormVO, HttpServletResponse response) {
		logger.debug("POST-save");
		JsonVO json = FormUtils.formVaild(staffFormVO);
		if (json.isSuccess()) {
			if (staffFormVO.getActionType().equals(FormUtils.ACTION_TYPE_ADD)) {
				json = staffService.addStaff(staffFormVO, SessionUtil.getCurrentSession());
			} else {
				json = staffService.alterStaff(staffFormVO, SessionUtil.getCurrentSession());
			}
		}
		ServletUtils.responseJson(response, json);
	}

	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	public void remveStaff(String ids, HttpServletResponse response) {
		logger.debug("POST-/staff/remove");
		JsonVO json = new JsonVO();
		boolean isSuccess = true;
		String message = "删除成功！";
		String[] idArr = ids.split(",");
		if (idArr != null && idArr.length > 0) {
			for (String id : idArr) {
				if (id.length() != 32) {
					isSuccess = false;
					message = FormUtils.ERROR_WORD_PARAMS_EXCEPTION;
				}
			}
		}
		if (isSuccess) {
			json = staffService.removeStaffsByIds(idArr);
			ServletUtils.responseJson(response, json);
			return;
		}
		json.setMessage(message);
		json.setSuccess(isSuccess);
		ServletUtils.responseJson(response, json);
	}

	@RequestMapping(value = "/pos/list", method = RequestMethod.GET)
	public String getStaffPosList(HttpServletResponse response) {
		return "staff_pos_list";
	}

	@RequestMapping(value = "/pos/map", method = RequestMethod.GET)
	public String getStaffPosMap(HttpServletResponse response) {
		return "staff_pos_map";
	}

}
